package gagnatech.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import gagnatech.entity.Media;
import gagnatech.service.ServiceMedia;

public class ControlerMedia {

	private ServiceMedia serviceMedia;
	
	private DataModel listMedia;
	private Media media;
	

	private Media editMedia;

	public ControlerMedia() {
		serviceMedia = new ServiceMedia();
		media = new Media();
	}
	
	public String editMedia() {
		editMedia= (Media) listMedia.getRowData();
		return "edit";
	}
	
	public String updateMedia() {
		serviceMedia.getManagerMedia().updateMedia(editMedia);
		listMedia.setWrappedData(serviceMedia.getManagerMedia().getAllMedia());
		return "list";
	}

	public DataModel getListMedia() {
		if (listMedia == null) {
			listMedia = new ListDataModel();
			listMedia.setWrappedData(serviceMedia.getManagerMedia().getAllMedia());
		}
		return listMedia;
	}

	public String deleteMedia() {
		Media media = (Media) listMedia.getRowData();
		serviceMedia.getManagerMedia().deleteMedia(media);
		listMedia.setWrappedData(serviceMedia.getManagerMedia().getAllMedia());
		return null;
	}
	
	public Media getEditMedia() {
		return editMedia;
	}

	public void setEditMedia(Media editMedia) {
		this.editMedia = editMedia;
	}

	/*
	public List<Media> getListMedia() {
		return serviceMedia.getManagerMedia().getAllMedia();
	}
*/
	public Media getMedia() {
		return media;
	}

	public void setMedia(Media media) {
		this.media = media;
	}

	public String createMedia() {
		media = new Media();
		serviceMedia.getManagerMedia().insertNewMedia(media);
		return "list";
	}
	
}
